home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / demos / surfdemo / surfthresh.c < prev    next >
C/C++ Source or Header  |  1997-07-10  |  12KB  |  358 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)surfthresh.c    V1.10    3/17/95";
  3. #endif
  4.  
  5. /*------------------------------------------------------------------
  6. | file name -- surfthresh.c
  7. |
  8. | Functions related to changing the color thresholds for the graph's.
  9. | The threshold control drawport is drawn over the graph, when this drawport
  10. | is active, all other picks are ignored.  The user must select OK or
  11. | CANCEL to remove the drawport.
  12. |
  13. | functions             Description
  14. | ---------             -----------
  15. | InitThreshControls     Initializes the input objects for chaning thresholds
  16. | DrawThreshControls     Draws the threshold control drawport
  17. |
  18. | HandleThreshControls   Handles all events inside the control drawport
  19. | EchoThreshBoxes          Echos the threshold choice in the threshold box
  20. |
  21. | change_color         Changes the colors in the selection boxes
  22. | init_slider         Initialize the slider for changing the threshold
  23. |
  24. |-----------------------------------------------------------------*/
  25.  
  26. #include "std.h"
  27. #include "dvstd.h"
  28. #include "dvtools.h"
  29. #include "VOstd.h"
  30. #include "dvGR.h"
  31. #include "Tfundecl.h"
  32. #include "surfdata.h"
  33. #include "dvinteract.h"
  34. #include "VOfundecl.h"
  35. #include "VUerfundecl.h"
  36. #include "VPfundecl.h"
  37. #include "surffundecl.h"
  38.  
  39.  
  40. /* Declare the various flags to be passed through the event handler */
  41. #define OK_CANCEL_EVENT    0
  42. #define PICK_EVENT    1
  43. #define CANCEL_VALUE     -1
  44. #define NO_INDEX     -1
  45. #define OK_CANCEL_MENU     "OkCancelMenu"
  46. #define SLIDER_NAME    "ThreshSlider"
  47. #define CHOICE_BOX_NAME    "Choice"
  48.  
  49. /*------------------------------------------------------------------
  50. | Local declarations
  51. |-----------------------------------------------------------------*/
  52. LOCAL CHAR *ColorName[]=
  53. {
  54.   "Color1",
  55.   "Color2",
  56.   "Color3",
  57.   "Color4",
  58. };
  59.  
  60. LOCAL OBJECT ThreshSlider;
  61.  
  62. LOCAL OBJECT InitForegroundColor;
  63. LOCAL OBJECT InitBackgroundColor;
  64.  
  65. LOCAL INT
  66.   ThreshMenuValue, CurrentThreshIndex;
  67. LOCAL FLOAT
  68.   ThreshSliderValue, SavedThreshValue[NUM_THRESHOLDS];
  69.  
  70. LOCAL VARDESC SliderVdp;
  71.  
  72. /***************** Begin Function Declarations *************/
  73. LOCAL  void EchoThreshBoxes V_P_((OBJECT input, int origin, int state, float *value, ADDRESS vdp, RECTANGLE *vp, ADDRESS *args));
  74. LOCAL  ADDRESS change_color V_P_((OBJECT obj, char *name, ADDRESS args));
  75. LOCAL  void init_slider V_P_((int index));
  76. /***************** End Function Declarations *************/
  77.  
  78. /*------------------------------------------------------------------
  79. |
  80. | InitThreshControls
  81. |       This functions sets up the service and echo functions for
  82. |       the changing the thresholds of the graph. Named areas are used
  83. |       to determine which threshold to change. A slider input object
  84. |       is used to get the new threhsold. A menu is used to know when to
  85. |
  86. */
  87. void InitThreshControls 
  88. V_P_ ((void))
  89. {
  90.   OBJECT menu, it;
  91.   VARDESC vdp;
  92.   ATTRIBUTES atts;
  93.  
  94.   /* Set flags */
  95.   ThreshActive = NO;
  96.  
  97.   /* Access the Embedded OK/CANCEL Menu */
  98.   menu = VOdrGetNamedObject (ThreshDrawing, OK_CANCEL_MENU);
  99.   vdp = GetVdp (menu);
  100.   (VOID) TvdPutBuffer (vdp, (ADDRESS) & ThreshMenuValue);
  101.   VPvdtype (vdp, V_I_TYPE);
  102.   (VOID) VUerServiceResultPost ((OBJECT) 1, HandleThreshControls,
  103.                                 (ADDRESS) NULL, 0,
  104.                                 menu, INPUT_DONE, OK_CANCEL_EVENT);
  105.  
  106.   /* Access the embedded slider */
  107.   ThreshSlider = VOdrGetNamedObject (ThreshDrawing, SLIDER_NAME);
  108.   SliderVdp = GetVdp (ThreshSlider);
  109.   (VOID) TvdPutBuffer (SliderVdp, (ADDRESS) & ThreshSliderValue);
  110.  
  111.   /* Set up Echo Function for the slider */
  112.   it = VOinTechnique (ThreshSlider, DONT_SET_THE_VALUE);
  113.   VOitPutEchoFunction (it, (VOITECHOFUNPTR)EchoThreshBoxes, (ADDRESS) NULL, 0);
  114.  
  115.   /* Save the slider's color, it's used when we draw the ThreshControls */
  116.   VOobAtGet (ThreshSlider, &atts);
  117.   InitForegroundColor = atts.foreground_color;
  118.   InitBackgroundColor = atts.background_color;
  119. }
  120.  
  121. /*------------------------------------------------------------------
  122. |
  123. | DrawThreshControls
  124. |       This functions draws the threshold control drawport. Before we
  125. |       draw the controls, we save the old threshold in case the user
  126. |       CANCELs the changes.  We also post a service function to
  127. |       capture all picks. This makes the user interact only with the
  128. |       control drawport.
  129. */
  130. void DrawThreshControls 
  131. V_P_ ((void))
  132. {
  133.   INT i;
  134.   ATTRIBUTES atts;
  135.  
  136.   /* Make Changes to the threhold boxes */
  137.   for (i = 0; i < NUM_COLORS; i++)
  138.     (VOID) TdrForEachNamedObject (ThreshDrawing, change_color, (ADDRESS) i);
  139.  
  140.   /* Save the old values in case of a CANCEL */
  141.   SetThresholds ();             /* found in surfthresh.c */
  142.   for (i = 0; i < NUM_THRESHOLDS; i++)
  143.     SavedThreshValue[i] = ThreshValue[i];
  144.  
  145.   /* Initialize event handling for the threshold controls...
  146.   |  Block out picks outside the drawport area, that way the user
  147.   |  can't interact with the other contorls.  This routine gets called
  148.   |  after other requested are posted, so this acts as a mask.
  149.   |
  150.   |  The HandleThreshControls will deal with erasing the control drawport
  151.   |  and clearing the posted event requests.
  152.   */
  153.   (VOID) VUerRectEdgePost ((OBJECT) THRESHOLD_COMMAND,
  154.                            HandleThreshControls, (ADDRESS) NULL, 0,
  155.                 (RECTANGLE *) NULL, (DV_BOOL) V_OUTSIDE, "", PICK_EVENT);
  156.  
  157.   /* Initialize the slider for the default case */
  158.   VPvd_drange (SliderVdp, (FLOAT) - 1, (FLOAT) 1);
  159.   VOobAtGet (ThreshSlider, &atts);
  160.   atts.foreground_color = InitForegroundColor;
  161.   atts.background_color = InitBackgroundColor;
  162.   VOobAtSet (ThreshSlider, &atts);
  163.  
  164.   /* Draw the Threshold Control Area */
  165.   (VOID) TdpDraw (ThreshDrawport);
  166.   ThreshActive = YES;
  167.  
  168.   /* Make the slider inactive until the user selects a threshold */
  169.   (VOID) VOinState (ThreshSlider, INACTIVE);
  170.  
  171. }
  172.  
  173. /*------------------------------------------------------------------
  174. |
  175. | EchoThreshBoxes
  176. |       This functions echos the threshold slider choice in the threshold
  177. |     if the user is currently changing a threshold.
  178. */
  179. /* ARGSUSED */
  180. LOCAL void 
  181. EchoThreshBoxes (input, origin, state, value, vdp, vp, args)
  182.      OBJECT input;
  183.      int origin;
  184.      int state;
  185.      float *value;
  186.      ADDRESS vdp;
  187.      RECTANGLE *vp;
  188.      ADDRESS *args;
  189. {
  190.   FLOAT factor;
  191.   INT i;
  192.  
  193.   if (origin == TAKE_INPUT)
  194.     {
  195.       /* Set the Threshold Value to the slider value */
  196.       ThreshValue[CurrentThreshIndex] = ThreshSliderValue;
  197.  
  198.       /* Determine the new values */
  199.       for (i = 0; i < NUM_THRESHOLDS; i++)
  200.         {
  201.           factor = (ThreshValue[i] - Formula.zmin) /
  202.             (Formula.zmax - Formula.zmin);
  203.           ThreshTable[i].upperlimit = (SHORT) (32767 * factor);
  204.         }
  205.  
  206.       /* Draw the threshold box */
  207.       DrawThreshRect (ThreshEchoArea);
  208.  
  209.       /* If we are done, quit echoing */
  210.       if (state == INPUT_DONE)
  211.         {
  212.           /* Make the slider inactive until the user selects a threshold */
  213.           (VOID) VOinState (ThreshSlider, INACTIVE);
  214.           CurrentThreshIndex = NO_INDEX;
  215.         }
  216.     }
  217. }
  218.  
  219. /*------------------------------------------------------------------
  220. |
  221. | HandleThreshControls
  222. |       This functions handles all picks inside the control drawport.
  223. |
  224. |       If the user selected inside the OK/CANCEL menu, if needed we
  225. |       restore the old thresholds and then erase the control and redraw
  226. |       the graph. We also clear the service function associated with
  227. |       masking all picks.
  228. |
  229. |       If the user made a pick inside the control drawport, we see
  230. |       if they have selected a new threshold to change.
  231. */
  232. /* ARGSUSED */
  233. int 
  234. HandleThreshControls (client, request, event_type, loc, args)
  235.      OBJECT client;          /* Input object initiating the call */
  236.      EVENT_REQUEST request;  /* Event request that triggered the call */
  237.      int event_type;         /* Label from the service result posting */
  238.      OBJECT loc;             /* Current location object */
  239.      ADDRESS args;           /* Arguments from the service result posting */
  240. {
  241.   FLOAT factor;       /* Used to compute the normalized threshold value */
  242.   INT i;
  243.  
  244.   if (event_type == OK_CANCEL_EVENT)
  245.     {
  246.       /* Determine if we use the old or new threshold values */
  247.       if (ThreshMenuValue == CANCEL_VALUE)
  248.         {
  249.           for (i = 0; i < NUM_THRESHOLDS; i++)
  250.             ThreshValue[i] = SavedThreshValue[i];
  251.         }
  252.  
  253.       /* Compute the new normalized threshold value */
  254.       for (i = 0; i < NUM_THRESHOLDS; i++)
  255.         {
  256.           factor = (ThreshValue[i] - Formula.zmin) /
  257.             (Formula.zmax - Formula.zmin);
  258.           ThreshTable[i].upperlimit = (SHORT) (32767 * factor);
  259.         }
  260.       DrawThreshRect (ThreshEchoArea);
  261.  
  262.       /* Remove Threshold Control Drawport and restore the graph */
  263.       CurrentThreshIndex = NO_INDEX;
  264.       (VOID) TdpErase (ThreshDrawport);
  265.       ThreshActive = NO;
  266.       DoPlot ();
  267.  
  268.       /* Clear the event requests related */
  269.       VUerClearAll ((OBJECT) THRESHOLD_COMMAND);
  270.     }
  271.   else
  272.     /* Must be a pick inside the control area */
  273.     {
  274.       OBJECT obj;
  275.       CHAR *name;
  276.  
  277.       obj = TloGetSelectedObject (loc);
  278.       if (obj)
  279.         {
  280.           name = TdrGetObjectName (ThreshDrawing, obj);
  281.           if (name && S_STRNCMP (name, "Choice", 6) == 0)
  282.             {
  283.               CurrentThreshIndex = (INT) S_ATOI (&name[6]) - 1;
  284.               init_slider (CurrentThreshIndex);
  285.             }
  286.         }
  287.     }
  288.   return DV_SUCCESS;
  289. }
  290.  
  291. /*------------------------------------------------------------------
  292. |
  293. | change_color
  294. |       This functions changes the colors associated with boxes for
  295. |    selecting which threshold to change.
  296. */
  297. LOCAL ADDRESS 
  298. change_color (obj, name, args)
  299.      OBJECT obj;
  300.      char *name;
  301.      ADDRESS args;
  302. {
  303.   INT index, color_index;
  304.   ATTRIBUTES atts;
  305.  
  306.   index = (INT) args;
  307.  
  308.   if (strcmp (name, ColorName[index]) == 0)
  309.     {
  310.       VOobAtGet (obj, &atts);
  311.       color_index = ThreshTable[index].threshcolor.color_index;
  312.       atts.foreground_color = VOcoCreate (COLOR_INDEX, (LONG) color_index,
  313.                                           (LONG) 0, (LONG) 0);
  314.       VOobAtSet (obj, &atts);
  315.     }
  316.  
  317.   return NULL;
  318. }
  319.  
  320. /*------------------------------------------------------------------
  321. |
  322. | init_slider
  323. |       This functions changes the color and range of the slider to
  324. |    match the threshold the user is changing or the default.
  325. */
  326. LOCAL void 
  327. init_slider (index)
  328.      int index;
  329. {
  330.  
  331.   DOUBLE lower, upper;
  332.   ATTRIBUTES atts;
  333.  
  334.   /* Set the initial value */
  335.   ThreshSliderValue = ThreshValue[index];
  336.  
  337.   /* Determine the range based on which threshold */
  338.   if (index == 0)
  339.     lower = Formula.zmin;
  340.   else
  341.     lower = ThreshValue[index - 1];
  342.   if (index == 2)
  343.     upper = Formula.zmax;
  344.   else
  345.     upper = ThreshValue[index + 1];
  346.   VPvd_drange (SliderVdp, lower, upper);
  347.  
  348.   /* Change the colors of the sliders, based on the threshold values */
  349.   VOobAtGet (ThreshSlider, &atts);
  350.   atts.foreground_color =
  351.     VOcoCreate ('i', ThreshTable[index].threshcolor.color_index);
  352.   atts.background_color =
  353.     VOcoCreate ('i', ThreshTable[index + 1].threshcolor.color_index);
  354.   VOobAtSet (ThreshSlider, &atts);
  355.   (VOID) TdpDrawObject (ThreshDrawport, ThreshSlider);
  356.   (VOID) VOinState (ThreshSlider, ACTIVE);
  357. }
  358.